1 00:00:01,230 --> 00:00:01,790 Okay. 2 00:00:01,800 --> 00:00:08,190 In this lecture, I'm going to show you the event data type role box has been designed to be event driven, 3 00:00:08,190 --> 00:00:10,920 meaning that code only executes when an event fires. 4 00:00:10,920 --> 00:00:12,360 And this is very performant. 5 00:00:12,960 --> 00:00:17,790 This means that you should never be polling for some condition or event to happen as that's just a complete 6 00:00:17,790 --> 00:00:19,170 waste of resources. 7 00:00:19,170 --> 00:00:24,120 You should never, ever have a loop in your game that just sits there and yields until some condition 8 00:00:24,120 --> 00:00:25,350 or event happens. 9 00:00:25,350 --> 00:00:26,400 That's just dumb. 10 00:00:26,730 --> 00:00:32,700 As an example here I have this repeat loop that's constantly yielding until some condition or an event 11 00:00:32,700 --> 00:00:34,770 fires to circumvent this. 12 00:00:34,770 --> 00:00:40,050 Roadblocks has the event data type which allows you to connect functions to the event that get executed 13 00:00:40,050 --> 00:00:41,310 when the event fires. 14 00:00:41,310 --> 00:00:46,560 So here in my game I have this part here called Event Part, and this part has several events that I 15 00:00:46,560 --> 00:00:48,270 can connect to and utilize. 16 00:00:48,970 --> 00:00:54,010 One of these events is called the Touched event, and it gets fired whenever this part gets touched 17 00:00:54,010 --> 00:00:54,910 by another part. 18 00:00:55,420 --> 00:01:00,700 Any functions connected to the event are called listeners, and when they get fired and executed, they're 19 00:01:00,700 --> 00:01:02,620 known as event handlers. 20 00:01:03,790 --> 00:01:08,470 So to use this event, I already have a reference to my part in the workspace. 21 00:01:08,950 --> 00:01:12,970 So I will index my part and get access to the touched event right here. 22 00:01:13,810 --> 00:01:17,020 Now I can use another function that's called Connect. 23 00:01:18,220 --> 00:01:21,910 And allows me to pass a function that connects to this event. 24 00:01:21,910 --> 00:01:25,120 And when the event fires, it executes the function. 25 00:01:25,240 --> 00:01:29,320 So I will create a lambda function here, an anonymous function. 26 00:01:29,440 --> 00:01:33,780 Remember, there is no identifier, so I can't call this function from anywhere else. 27 00:01:33,790 --> 00:01:35,890 It just belongs to this event. 28 00:01:36,670 --> 00:01:43,350 And then I can print the name of the part that touched this other part, and it gets automatically passed 29 00:01:43,350 --> 00:01:45,090 to my lambda function as an argument. 30 00:01:45,100 --> 00:01:47,410 So I'll just give it a name like other part. 31 00:01:48,600 --> 00:01:51,030 And then I can print the name of this other part. 32 00:01:52,320 --> 00:01:59,070 So now if I go into my game and I actually play my game, doing play, it spawns my character in. 33 00:02:04,290 --> 00:02:04,850 There we go. 34 00:02:05,180 --> 00:02:07,490 I can go run over to my part. 35 00:02:10,730 --> 00:02:12,800 Looks like Roblox had an error there. 36 00:02:13,760 --> 00:02:18,770 And when I touched my part, it should fire the name of one of my limbs that touched the part. 37 00:02:19,610 --> 00:02:20,210 Here we go. 38 00:02:20,240 --> 00:02:20,750 Right. 39 00:02:20,750 --> 00:02:21,520 Lower leg. 40 00:02:21,530 --> 00:02:23,120 Right upper leg and right foot. 41 00:02:23,420 --> 00:02:25,070 And then I can keep jumping on it. 42 00:02:26,060 --> 00:02:28,340 So, like, I got a humanoid part touching it. 43 00:02:29,570 --> 00:02:32,090 So you see all the stuff printing when I touch the part. 44 00:02:33,570 --> 00:02:38,610 Now, another event I want to talk about is the child and descendent added events. 45 00:02:39,030 --> 00:02:44,580 And basically these events get fired when an instance becomes a child of the object you're listening 46 00:02:44,580 --> 00:02:45,060 to. 47 00:02:45,970 --> 00:02:51,610 The child added event is specifically for listening when a non nested child gets added to my part. 48 00:02:51,820 --> 00:02:57,760 So for example, in my part here, if I added some kind of instance like a texture, the event will 49 00:02:57,760 --> 00:02:58,300 get fired. 50 00:02:58,300 --> 00:03:00,880 And it would say, This texture instance got added to my part. 51 00:03:02,020 --> 00:03:07,960 If I added another child whose parent was the texture, like, I'll add, I don't know, an animation. 52 00:03:08,820 --> 00:03:13,380 Then the child added event would get fired for this texture, but it wouldn't get it fired for this 53 00:03:13,380 --> 00:03:13,920 part. 54 00:03:14,430 --> 00:03:20,760 If I wanted to listen to every single nested child get added to my part, then I would use the descendant 55 00:03:20,760 --> 00:03:23,280 added event and I'll show it here real quick. 56 00:03:25,460 --> 00:03:31,850 So reference my part again and then find the child added event and then connect a lambda function to 57 00:03:31,850 --> 00:03:32,150 it. 58 00:03:33,140 --> 00:03:36,320 And of course, this event passes a new child. 59 00:03:37,480 --> 00:03:39,460 And then I'll just print the name of the child. 60 00:03:41,780 --> 00:03:49,190 And then for the descendent added event, connect another function and get the descendant. 61 00:03:51,380 --> 00:03:53,390 And then I'll just print the name of the descendant. 62 00:03:54,470 --> 00:03:55,670 Now my script. 63 00:03:56,090 --> 00:03:59,420 I'm going to create a for loop that just loops twice. 64 00:04:02,000 --> 00:04:03,980 And I'm going to create a new part. 65 00:04:05,250 --> 00:04:08,430 Using the instance, start new function and make a new part. 66 00:04:08,760 --> 00:04:10,830 Not particle emitter, but a part. 67 00:04:11,810 --> 00:04:17,390 And then the part, the name of this part will just be new part. 68 00:04:18,510 --> 00:04:20,610 And I'll concatenate to the string I. 69 00:04:22,120 --> 00:04:28,120 And then I'll set the parent of new part to be the part. 70 00:04:30,070 --> 00:04:35,560 Now, the problem here is because I'm studying the parent of these new parts to the same part. 71 00:04:35,680 --> 00:04:38,680 Both of these events will fire twice. 72 00:04:38,830 --> 00:04:44,620 This is because the child added event will see both parts being added because there are no nested children. 73 00:04:44,800 --> 00:04:50,110 And this event will also see both children being added because it's looking for every single descendant. 74 00:04:50,410 --> 00:04:56,080 Now, if I want this event to fire twice, but only have this event fire once, I need to make one of 75 00:04:56,080 --> 00:05:02,350 these new parts nested or basically the parent of one of the new parts needs to be the other new part. 76 00:05:02,530 --> 00:05:05,530 And to do that, I have this neat little function I can access. 77 00:05:05,530 --> 00:05:09,550 And it's called the Fine First Child, which is a and it returns. 78 00:05:09,550 --> 00:05:12,040 The first child of this function can fine. 79 00:05:12,040 --> 00:05:15,850 That is a child of this part that belongs to particular class. 80 00:05:15,850 --> 00:05:20,770 In this case, I want to do the base part class because these new instances are parts. 81 00:05:21,580 --> 00:05:27,040 And if it can't find a part because through the first iteration there will be no children to our part 82 00:05:27,040 --> 00:05:28,360 that are parts. 83 00:05:28,660 --> 00:05:33,550 Then I can use the or operator here the or statement and just pass part. 84 00:05:33,970 --> 00:05:38,410 So through the first iteration of this loop, this will result in nil. 85 00:05:38,410 --> 00:05:39,520 So it will be falsey. 86 00:05:39,520 --> 00:05:42,460 So it'll default to parenting to part. 87 00:05:43,240 --> 00:05:50,920 But once we create this new part in our part, so when a new part becomes a child of our event part, 88 00:05:51,040 --> 00:05:52,180 I'll just make one here. 89 00:05:52,420 --> 00:05:55,630 So we have a new child in here through the second iteration. 90 00:05:55,630 --> 00:06:01,300 When this function gets run again, it'll see this new part exists, so then it will set the parent 91 00:06:01,300 --> 00:06:02,140 to this new part. 92 00:06:02,140 --> 00:06:05,410 So the second part will be the parent of this part. 93 00:06:05,530 --> 00:06:08,710 And now we have a nested child that is part of our event, part. 94 00:06:08,980 --> 00:06:15,280 So this means that the descendant added event will see both parts being added, but the child added 95 00:06:15,280 --> 00:06:17,080 won't be able to see both parts. 96 00:06:17,080 --> 00:06:18,790 It'll only see the first one. 97 00:06:21,330 --> 00:06:23,010 Let's run the game to confirm this. 98 00:06:25,530 --> 00:06:26,250 Perfect. 99 00:06:26,520 --> 00:06:32,130 You see that the child added event saw a new part one and so did the descendant added event. 100 00:06:32,490 --> 00:06:38,610 But then when new part two was created, it got parented to new part one, so only the descendant added 101 00:06:38,610 --> 00:06:40,860 event saw it and not the child added event. 102 00:06:42,900 --> 00:06:48,060 So this is just a quick overview of the event data type and how you can utilize them in your game. 103 00:06:48,300 --> 00:06:53,400 The event data type is among the most common data types you will be using in Roblox.